#include "CTransitionScreen.h"

#include "..//Wrappers//CSGD_TextureManager.h"
#include "CGame.h"
#include "IGameState.h"
#include "CSoundManager.h"


#define DOOR_SPEED 700
CTransitionScreen::CTransitionScreen()
{
	CSGD_TextureManager * pTM = CSGD_TextureManager::GetInstance();
	
	m_tDoorFrame.Setup(pTM->LoadTexture("Resource/ScreenImages/TransitionScreen/BS_DoorFrame.png"),0,0,0,0);
	m_tLeftDoor.Setup(pTM->LoadTexture("Resource/ScreenImages/TransitionScreen/BS_LeftDoor.png"),0,0,0,0);
	m_tRightDoor.Setup(pTM->LoadTexture("Resource/ScreenImages/TransitionScreen/BS_RightDoor.png"),0,0,0,0);
	m_tTVFrame.Setup(pTM->LoadTexture("Resource/ScreenImages/TransitionScreen/BS_TVFrame.png"),0,0,0,0);
	m_tTVScreen.Setup(pTM->LoadTexture("Resource/ScreenImages/TransitionScreen/BS_TVScreen.png"),0,0,0,0);
	
	m_bIsOpenning = true;
	m_bIsClosed = true;
	m_bIsStopped = false;

}

CTransitionScreen::~CTransitionScreen(void)
{

}

void CTransitionScreen::Update(float fElapsedTime)
{

		//Door closing
		if (m_tLeftDoor.m_nXPos > 0 && m_tLeftDoor.m_nXVel > 0 )
		{
			m_tLeftDoor.m_nXVel = 0;
			m_tTVFrame.m_nXVel = 0;
			m_tTVScreen.m_nXVel = 0;


			m_tLeftDoor.m_nXPos = 0;
			m_tTVFrame.m_nXPos = 0;
			m_tTVScreen.m_nXPos = 0;
			m_bIsClosed = true;
			m_bIsStopped = true;


		}
		
		if (m_tRightDoor.m_nXPos < 0 && m_tRightDoor.m_nXVel < 0 )
		{
			m_tRightDoor.m_nXPos = 0;
			m_tRightDoor.m_nXVel = 0;
			m_bIsClosed = true;
				m_bIsStopped = true;
		}

		//Door opening
		if ( m_tLeftDoor.m_nXPos < -500 && m_tLeftDoor.m_nXVel < 0 )
		{
			m_tLeftDoor.m_nXVel = 0;
			m_tTVFrame.m_nXVel = 0;
			m_tTVScreen.m_nXVel = 0;
			m_bIsClosed = false;
			m_bIsStopped = true;

		}
		if (m_tRightDoor.m_nXPos > 500 && m_tRightDoor.m_nXVel > 0)
		{
			m_tRightDoor.m_nXVel = 0;
			m_bIsClosed = false;
			m_bIsStopped = true;

			
		}

	
		m_tDoorFrame.Update(fElapsedTime);
		m_tLeftDoor.Update(fElapsedTime);
		m_tRightDoor.Update(fElapsedTime);
		m_tTVFrame.Update(fElapsedTime);
		m_tTVScreen.Update(fElapsedTime); 



	


	
}
void CTransitionScreen::OpenDoor()
{

	
	m_bIsOpenning = true;

	m_tLeftDoor.m_nXVel = -DOOR_SPEED;
	m_tRightDoor.m_nXVel = DOOR_SPEED;
	m_tTVFrame.m_nXVel = -DOOR_SPEED;
	m_tTVScreen.m_nXVel = -DOOR_SPEED;
	
	m_bIsStopped = false;

}

void CTransitionScreen::CloseDoor()
{

	CSoundManager::GetInstance()->PlaySoundIndxBuffered(SOUND_CLOSE_DOOR);
	m_bIsOpenning = false;
	m_bIsStopped = false;

	m_tLeftDoor.m_nXVel = DOOR_SPEED;
	m_tRightDoor.m_nXVel = -DOOR_SPEED; 
	m_tTVFrame.m_nXVel = DOOR_SPEED;
	m_tTVScreen.m_nXVel = DOOR_SPEED;

}

void CTransitionScreen::Render()
{
	CSGD_TextureManager * pTM = CSGD_TextureManager::GetInstance();


	pTM->Draw(m_tLeftDoor.m_nImageID,m_tLeftDoor.m_nXPos,m_tLeftDoor.m_nYPos
		,1.0f,1.0f,NULL,0.0f,0.0f,0.0f,0xFFFFFFFF);
	pTM->Draw(m_tRightDoor.m_nImageID,m_tRightDoor.m_nXPos,m_tRightDoor.m_nYPos
		,1.0f,1.0f,NULL,0.0f,0.0f,0.0f,0xFFFFFFFF);
	pTM->Draw(m_tTVScreen.m_nImageID,m_tTVScreen.m_nXPos,m_tTVScreen.m_nYPos
		,1.0f,1.0f,NULL,0.0f,0.0f,0.0f,0xFFFFFFFF);
	pTM->Draw(m_tTVFrame.m_nImageID,m_tTVFrame.m_nXPos,m_tTVFrame.m_nYPos
		,1.0f,1.0f,NULL,0.0f,0.0f,0.0f,0xFFFFFFFF);
	pTM->Draw(m_tDoorFrame.m_nImageID,m_tDoorFrame.m_nXPos,m_tDoorFrame.m_nYPos
		,1.0f,1.0f,NULL,0.0f,0.0f,0.0f,0xFFFFFFFF);
	

}

void CTransitionScreen::ResetDoors()
{
	m_tDoorFrame.m_nXPos = 0;
	m_tLeftDoor.m_nXPos = 0;
	m_tRightDoor.m_nXPos = 0;
	m_tTVFrame.m_nXPos = 0;
	m_tTVScreen.m_nXPos = 0;

}

void CTransitionScreen::ChangeTo(IGameState * cState)
{

	CloseDoor();
	if (GetIsClosed())
	{	
		//CSoundManager::GetInstance()->PlaySoundIndxInterupt(SOUND_METAL);
		CGame::GetInstance()->ChangeState(cState);  
		CSoundManager::GetInstance()->PlaySoundIndxBuffered(SOUND_OPEN_DOOR);
		//OpenDoor();
	}
	
}